import {defineStore} from "pinia";
import projectSetting from "@/settings/project";
import store from "@/store/store";
import {dictTypeList} from "@/api/common";
import {membersInfoList} from "@/api/customerArchives/memberManagement";

const useAppStore = defineStore("appStore", {
  state() {
    return {
      loading: false, //全局loading开关
      membershipList: [],
      pageCommonInfo: {
        //页面公用参数
        canUseHeight: 0 //页面可用高度 用于设置页面表格滚动高度
      },
      menuType: localStorage.getItem("menuType")
        ? localStorage.getItem("menuType")
        : projectSetting.menuType,
      dicList: []
    };
  },
  actions: {
    setMenuType() {
      //切换菜单类型
      if (this.menuType == "card") {
        this.menuType = "accordion";
        localStorage.setItem("menuType", "accordion");
      } else {
        this.menuType = "card";
        localStorage.setItem("menuType", "card");
      }
    },
    loadDicList() {
      // 加载字典列表
      dictTypeList().then((res) => {
        localStorage.setItem("dicList", JSON.stringify(res.data));
        this.dicList = res.data;
      });
    },
    //获取会员
    async getMembershipList(flag = false) {
      if (this.membershipList.length > 0 && !flag) {
        return this.membershipList;
      } else {
        let data = await membersInfoList();
        this.membershipList = data.data;
        return this.membershipList;
      }
    }
  }
});

export default useAppStore(store);
